package com.kiku.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiku.reggie.common.CustomException;
import com.kiku.reggie.entity.Category;
import com.kiku.reggie.entity.Dish;
import com.kiku.reggie.entity.Setmeal;
import com.kiku.reggie.entity.SetmealDish;
import com.kiku.reggie.mapper.CategoryMapper;
import com.kiku.reggie.service.CategoryService;
import com.kiku.reggie.service.DishService;
import com.kiku.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @className: CategoryServiceImpl
 * @description: TODO 类描述
 * @author: kiku
 * @date: 2023/10/19 20:54
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;

    //实现根据id删除分类，在删除前进行判断是否关联了套餐或菜品
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品,若已经关联，就抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        //进行查询
        int dishCount = dishService.count(dishLambdaQueryWrapper);

        if (dishCount>0){
            //已经关联了菜品，抛出一个业务异常
            throw new CustomException("已经关联了菜品，删除失败");
        }

        //查询当前分类是否关联了套餐,若已经关联，就抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int setMealCount = setMealService.count(setmealLambdaQueryWrapper);

        if (setMealCount>0){
            //已经关联了套餐，抛出一个业务异常
            throw new CustomException("已经关联了套餐，删除失败");
        }

        //正常删除(super调用的是IService的方法)
        super.removeById(id);





    }
}
